<template>
  <BasicCard>
    <BasicVxeTable v-bind="gridOptions"> </BasicVxeTable>
  </BasicCard>
</template>

<script setup lang="ts">
  import dayjs from 'dayjs'
  import { columnsStacking } from '../data'
  import { splitTsIdToTime } from '@/utils'
  import { getDaBaoJiPingZhongChengDuo } from '@/api/shuJuBaoBiao'

  const { t } = useI18n()

  const gridOptions = reactive<BasicGridProps>({
    extraConfig: {
      title: t(`custom.品种成垛报表`),
      isCustom: false,
      isExport: false,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `shujubaobiao-pinzhongchengduo`, // id用于缓存,务必配置
    height: '100%',
    loading: true,
    showFooter: true,
    columns: columnsStacking,
    toolbarConfig: {}, // 工具栏
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return '合计'
          } else if (columnIndex === 5) {
            return handleSum(data, 'count')
          } else if (columnIndex === 6) {
            return handleSum(data, 'captureCount')
          } else {
            return '--'
          }
        })
      ]
    }
  })

  const handleSum = (data, key) => {
    let count = 0
    data.forEach((item) => {
      count += item[key]
    })
    return '合计:' + count
  }

  const getList = () => {
    getDaBaoJiPingZhongChengDuo({
      TsIdStart: 25072202,
      TsIdEnd: 25072202
    })
      .then((res: any) => {
        gridOptions.loading = false
        gridOptions.data = res.map((value) => {
          return {
            tsid: splitTsIdToTime(value.tsid) + ' ' + value.banCi,
            packerName: value.packerName,
            count: value.count,
            materialName: value.materialName,
            time: value.time == null ? null : dayjs(value.time).format('YYYY-MM-DD HH:mm:ss'),
            thirdMaterialColorDescription:
              value.materialName == null ? '' : value.thirdMaterialColorDescription,
            duoWei: value.duoWei,
            pinZhong: value.pinZhong,
            materialColor: value.materialColor,
            lineNum: value.lineNum,
            captureCount: value.captureCount,
            chengDuoHao: value.lineNum + '-' + (value.duoWei == null ? '' : value.duoWei)
          }
        })
      })
      .catch(() => {
        gridOptions.loading = false
      })
  }

  onMounted(() => {
    getList()
  })
</script>
